//
//  WebViewHandle.m
//  carcareIOS
//
//  Created by ileo on 16/7/25.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "WebViewHandle.h"
#import "ShareCenter.h"
#import "ShareItemView.h"
#import "CommitInfoViewController.h"
#import "TakePhotoTestViewController.h"
#import "NetInsurance.h"

@implementation WebViewHandle

+(BOOL)handleURL:(NSURL *)url withViewController:(BaseWebViewController *)webVC{
    if ([url.scheme isEqualToString:@"icz"]) {
        NSDictionary *params = [WebViewHandle paramsFromURL:url.absoluteString];
        NSString *host = url.host;
        if ([host isEqualToString:@"app_share"]) {
            [WebViewHandle appShareWithParams:params webVC:webVC];
        }else if ([host isEqualToString:@"app_upload_img"]) {
            [WebViewHandle appUploadImgWithParams:params webVC:webVC];
        }else if ([host isEqualToString:@"upload_insurance_img"]) {
            [WebViewHandle uploadInsuranceImgWithParams:params webVC:webVC];
        }else if ([host isEqualToString:@"upload_insurance_info"]) {
            [WebViewHandle uploadInsuranceInfoWithParams:params webVC:webVC];
        }else if ([host isEqualToString:@"leave_webview"]) {
            [WebViewHandle leaveWebViewWithParams:params webVC:webVC];
        }
        return NO;
    }
    return YES;
}

+(NSString *)url:(NSString *)url addParams:(NSDictionary *)params{
    if (![url isNotNil]) {
        return nil;
    }
    NSMutableString *newUrl = [NSMutableString stringWithString:url];
    if ([url componentsSeparatedByString:@"?"].count == 1) {
        [newUrl appendString:@"?"];
    }else{
        [newUrl appendString:@"&"];
    }
    [[params allKeys] enumerateObjectsUsingBlock:^(NSString * _Nonnull key, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx != 0) {
            [newUrl appendString:@"&"];
        }
        [newUrl appendFormat:@"%@=%@",key,params[key]];
    }];
    return [newUrl copy];
}

+(NSString *)urlAddUserParamsWithURL:(NSString *)url{
    return [self url:url addParams:@{@"icz_uid" : @(USERINFO.userID), @"icz_key" : USERINFO.iczkey}];
}


+(NSDictionary *)paramsFromURL:(NSString *)url{
    NSArray *arr = [url componentsSeparatedByString:@"?"];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:3];
    if (arr.count > 1) {
        NSArray *params = [arr[1] componentsSeparatedByString:@"&"];
        [params enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSArray *vk = [obj componentsSeparatedByString:@"="];
            if (vk.count > 1) {
                NSString *key = vk[0];
                NSString *value = [[obj substringFromIndex:key.length + 1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                [dic setObject:value forKey:key];
            }
        }];
    }
    return [dic copy];
}

+(void)dissmissViewController:(UIViewController *)viewController{
    if (viewController.navigationController.viewControllers.count > 1) {
        [viewController.navigationController popViewControllerAnimated:YES];
    }else{
        [viewController dismissViewControllerAnimated:YES completion:nil];
    }
}

#pragma mark - host

+(void)leaveWebViewWithParams:(NSDictionary *)params webVC:(BaseWebViewController *)webVC{
    [WebViewHandle dissmissViewController:webVC];
}

+(void)appShareWithParams:(NSDictionary *)params webVC:(BaseWebViewController *)webVC{
    ShareItemView *sview = [[ShareItemView alloc] initWithContents:@[@(Share_WX_Session),@(Share_WX_Timeline),@(Share_QQ)] Click:^(NSInteger type) {
        ShareURLModel *model = [[ShareURLModel alloc] init];
        model.title = params[@"title"];
        model.detail = params[@"detail"];
        model.shareURL = params[@"shareURL"];
        model.imageURL = params[@"imageURL"];
        [[ShareCenter sharedInstance] shareURL:model withPlatform:type callback:^(BOOL isSuccess) {
            if (params[@"jumpURL"]) {
                NSString *jump = [NSString stringWithFormat:@"%@?icz_uid=%zd&icz_key=%@&isSuccess=%zd&platform=%zd",params[@"jumpURL"],USERINFO.userID,USERINFO.iczkey,isSuccess?1:0,type];
                [webVC reloadURL:jump];
            }
        }];
        [AlertMgr dismiss:TYPE_VIEW_POPUP];
    } cancel:^{
        [AlertMgr dismiss:TYPE_VIEW_POPUP];
    }];
    [AlertMgr showPopupView:sview bottom:0];
}

+(void)appUploadImgWithParams:(NSDictionary *)params webVC:(BaseWebViewController *)webVC{
    CommitInfoViewController *commitVC = [[CommitInfoViewController alloc] initWithTableRefreshType:TYPE_TABLE_REFRESH_NONE];
    commitVC.UploadSuccess = ^(NSString *url){
        NSInteger uid = USERINFO.userID;
        NSString *key = USERINFO.iczkey;
        NSString *jump = [NSString stringWithFormat:@"%@?icz_uid=%zd&icz_key=%@&icz_imgType=%@&icz_imageUrl=%@",params[@"jump_url"],uid,key,params[@"icz_imgType"],url];
        [webVC reloadURL:jump];
    };
    commitVC.paramsDic = params;
    [webVC presentViewController:[UINavigationController navigationControllerWithRootViewController:commitVC] animated:YES completion:nil];
}

+(void)uploadInsuranceImgWithParams:(NSDictionary *)params webVC:(BaseWebViewController *)webVC{
    TakePhotoTestViewController *vc = [[TakePhotoTestViewController alloc] initWithTableRefreshType:TYPE_TABLE_REFRESH_NONE];
    vc.UploadSuccess = ^(NSString *img){
        [NetInsurance insuranceUploadPolicyImg:img finish:^{
            [WebViewHandle dissmissViewController:webVC];
        }];
    };
    [webVC presentViewController:[UINavigationController navigationControllerWithRootViewController:vc] animated:YES completion:nil];
}

+(void)uploadInsuranceInfoWithParams:(NSDictionary *)params webVC:(BaseWebViewController *)webVC{
    [NetInsurance insuranceCheckInfo:params finish:^{
        [WebViewHandle dissmissViewController:webVC];
    }];
}

@end
